﻿using Dapper;
using System;
using BCEntity.TMS.OrderExtentionGoodsChecklist;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.OrderExtentionGoodsChecklist
{
	/// <summary>
	/// 订单扩展货物清单信息
	/// </summary>
	public class OrderExtentionGoodsChecklistData : IOrderExtentionGoodsChecklistData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM orderextentiongoodschecklist";
		private const string QUERY = @"SELECT OrderExtentionGoodsChecklistId,GoodsWidth,GoodsHeight,GoodsCount,GoodsPrice,GoodsUnit,CreateTime,OrderId,OrderNo,
											GoodsSku,GoodsPhoto,GoodsName,GoodsWeight,GoodsVolume,GoodsLength 
										FROM orderextentiongoodschecklist";
		public OrderExtentionGoodsChecklistData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查订单扩展货物清单信息是否存在
		public async Task<bool> Exists(long orderExtentionGoodsChecklistId)
		{
			string sql = string.Format(@"{0} WHERE OrderExtentionGoodsChecklistId=?OrderExtentionGoodsChecklistId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderExtentionGoodsChecklistId", orderExtentionGoodsChecklistId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加订单扩展货物清单信息
		public async Task<bool> Add(List<OrderExtentionGoodsChecklistEntity> entities)
		{
			return await this.databaseContext.Master.InsertAllAsync(entities, databaseContext.Transaction) > 0;
		}

		public async Task<OrderExtentionGoodsChecklistEntity> Add(OrderExtentionGoodsChecklistEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("orderextentiongoodschecklist", new string[] { "GoodsWidth", "GoodsHeight", "GoodsCount", "GoodsPrice", "GoodsUnit", 
				"OrderId", "OrderNo", "GoodsSku", "GoodsPhoto", "GoodsName", "GoodsWeight", "GoodsVolume", "GoodsLength" });
			sql = string.Format("{0} {1} WHERE OrderExtentionGoodsChecklistId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?GoodsWidth", entity.GoodsWidth, DbType.Decimal, size: 10);
			parameters.Add("?GoodsHeight", entity.GoodsHeight, DbType.Decimal, size: 10);
			parameters.Add("?GoodsCount", entity.GoodsCount, DbType.Int32, size: 11);
			parameters.Add("?GoodsPrice", entity.GoodsPrice, DbType.Decimal, size: 10);
			parameters.Add("?GoodsUnit", entity.GoodsUnit?.Trim(), DbType.AnsiString, size: 10);
			parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
			parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?GoodsSku", entity.GoodsSku?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?GoodsPhoto", entity.GoodsPhoto?.Trim(), DbType.String);
			parameters.Add("?GoodsName", entity.GoodsName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?GoodsWeight", entity.GoodsWeight, DbType.Decimal, size: 10);
			parameters.Add("?GoodsVolume", entity.GoodsVolume, DbType.Decimal, size: 10);
			parameters.Add("?GoodsLength", entity.GoodsLength, DbType.Decimal, size: 10);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderExtentionGoodsChecklistEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改订单扩展货物清单信息
		public async Task<OrderExtentionGoodsChecklistEntity> Update(OrderExtentionGoodsChecklistEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("orderextentiongoodschecklist", new string[] { "GoodsWidth", "GoodsHeight", "GoodsCount", "GoodsPrice", "GoodsUnit", 
				"OrderId", "OrderNo", "GoodsSku", "GoodsPhoto", "GoodsName", "GoodsWeight", "GoodsVolume", "GoodsLength" }, new string[] { "OrderExtentionGoodsChecklistId" });
			sql = string.Format("{0} {1} WHERE OrderExtentionGoodsChecklistId=?OrderExtentionGoodsChecklistId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderExtentionGoodsChecklistId", entity.OrderExtentionGoodsChecklistId, DbType.Int64, size: 20);
			parameters.Add("?GoodsWidth", entity.GoodsWidth, DbType.Decimal, size: 10);
			parameters.Add("?GoodsHeight", entity.GoodsHeight, DbType.Decimal, size: 10);
			parameters.Add("?GoodsCount", entity.GoodsCount, DbType.Int32, size: 11);
			parameters.Add("?GoodsPrice", entity.GoodsPrice, DbType.Decimal, size: 10);
			parameters.Add("?GoodsUnit", entity.GoodsUnit?.Trim(), DbType.AnsiString, size: 10);
			parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
			parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?GoodsSku", entity.GoodsSku?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?GoodsPhoto", entity.GoodsPhoto?.Trim(), DbType.String);
			parameters.Add("?GoodsName", entity.GoodsName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?GoodsWeight", entity.GoodsWeight, DbType.Decimal, size: 10);
			parameters.Add("?GoodsVolume", entity.GoodsVolume, DbType.Decimal, size: 10);
			parameters.Add("?GoodsLength", entity.GoodsLength, DbType.Decimal, size: 10);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderExtentionGoodsChecklistEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除订单扩展货物清单信息
		public async Task<bool> Delete(long orderExtentionGoodsChecklistId)
		{
			string sql = @"DELETE FROM orderextentiongoodschecklist WHERE OrderExtentionGoodsChecklistId=?OrderExtentionGoodsChecklistId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderExtentionGoodsChecklistId", orderExtentionGoodsChecklistId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取订单扩展货物清单信息
		public async Task<OrderExtentionGoodsChecklistEntity> GetEntity(long orderExtentionGoodsChecklistId)
		{
			string sql = string.Format(@"{0} WHERE OrderExtentionGoodsChecklistId=?OrderExtentionGoodsChecklistId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderExtentionGoodsChecklistId", orderExtentionGoodsChecklistId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderExtentionGoodsChecklistEntity>(sql, parameters);
		}
		#endregion

		#region 获取订单扩展货物清单信息列表
		public async Task<IEnumerable<OrderExtentionGoodsChecklistEntity>> GetList(string orderNo)
		{
			string sql = string.Format("{0} WHERE OrderNo=?OrderNo", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("OrderNo", orderNo, DbType.AnsiString);
			return await this.databaseContext.Master.QueryAsync<OrderExtentionGoodsChecklistEntity>(sql, parameters);
		}
		public async Task<IEnumerable<OrderExtentionGoodsChecklistEntity>> GetList(long orderId)
		{
			string sql = string.Format("{0} WHERE OrderId=?OrderId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("OrderId", orderId, DbType.Int64);
			return await this.databaseContext.Master.QueryAsync<OrderExtentionGoodsChecklistEntity>(sql, parameters);
		}
		#endregion

		#region 获取订单扩展货物清单信息分页
		public async Task<(IEnumerable<OrderExtentionGoodsChecklistEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM orderextentiongoodschecklist {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<OrderExtentionGoodsChecklistEntity, int>(sql, parameters);
		}

        #endregion
    }
}



